package com.lightsaber.trade.cloth.service.planning.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lightsaber.trade.core.dao.BaseDao;
import com.lightsaber.trade.core.entity.AuditedObject;
import com.lightsaber.trade.core.entity.BaseEntity;
import com.lightsaber.trade.core.service.HppBaseServiceImpl;
import com.lightsaber.trade.cloth.dao.planning.Level2BreakDownDao;
import com.lightsaber.trade.cloth.entity.planning.Level2BreakDown;
import com.lightsaber.trade.cloth.entity.planning.ProgrammePlanningItem;
import com.lightsaber.trade.cloth.service.planning.Level2BreakDownService;
import com.lightsaber.trade.cloth.service.planning.ProgrammePlanningItemService;
import com.lightsaber.trade.cloth.service.programme.OfferingCampusManager;

@Service
@Component
@Transactional
public class Level2BreakDownServiceImpl extends HppBaseServiceImpl<Level2BreakDown, String>
        implements Level2BreakDownService {

    @Autowired
    private Level2BreakDownDao level2BreakDownDao;
    @Autowired
    private transient ProgrammePlanningItemService ppiService;

    @Override
    protected BaseDao<Level2BreakDown, String> getDao() {
        return level2BreakDownDao;
    }

    @Autowired
    OfferingCampusManager campusManager;

    @Override
    public void save(final Level2BreakDown level2BreakDown) {
        if (StringUtils.isBlank(level2BreakDown.getId())) {
            String toSaveSemester = level2BreakDown.getSemester();
            List<Level2BreakDown> breakDowns = this.findByProgrammePlanningItemId(level2BreakDown
                    .getProgrammePlanningItem().getId());
            if (breakDowns != null && breakDowns.size() > 0) {
                String semester;
                for (Level2BreakDown breakDown : breakDowns) {
                    if (breakDown == null) {
                        continue;
                    }
                    semester = breakDown.getSemester();
                    if (toSaveSemester.equals(semester)) {
                        return;
                    }
                }
            }

            level2BreakDownDao.save(level2BreakDown);
        } else {
            Level2BreakDown old = get(level2BreakDown.getId());
            BeanUtils.copyProperties(level2BreakDown, old, new String[] { BaseEntity.ID,
                    BaseEntity.VERSION, AuditedObject.CREATED_BY, AuditedObject.CREATED_DATE,
                    "programmePlanningItem" });
            level2BreakDownDao.update(old);
        }

    }

    public void deleteByProgrammePlanningItemId(final String ppiId) {
        level2BreakDownDao.deleteByProgrammePlanningItemId(ppiId);
    }

    @Override
    public void saveBreakDownList(final ProgrammePlanningItem ppi) {

        List<Level2BreakDown> level2BreakDownList = ppi.getLevel2BreakDownList();

        deleteByProgrammePlanningItemId(ppi.getId());

        if (level2BreakDownList != null) {
            for (int j = 0; j < level2BreakDownList.size(); j++) {
                Level2BreakDown bd = level2BreakDownList.get(j);
                if (bd == null) {
                    continue;
                }
                Level2BreakDown db_new = new Level2BreakDown();
                db_new.setPlannedPlaces(bd.getPlannedPlaces());
                db_new.setProgrammePlanningItem(ppiService.get(ppi.getId()));
                db_new.setSemester(bd.getSemester());
                save(db_new);
            }
        }
    }

    @Override
    public List<Level2BreakDown> findByProgrammePlanningItemId(final String ppiId) {
        return level2BreakDownDao.findByProgrammePlanningItemId(ppiId);
    }

}
